package com.example.demo.service.Impl;

import com.example.demo.entity.Application;
import com.example.demo.entity.Progress;
import com.example.demo.entity.Student;
import com.example.demo.mapper.*;
import com.example.demo.service.ApplicationService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class ApplicationImpl implements ApplicationService {
    private final ApplicationMapper applicationMapper;
    private final LicenceMapper licenceMapper;
    private final CoachMapper coachMapper;
    private final StudentMapper studentMapper;
    private final StudentAccountMapper studentAccountMapper;
    private final ProgressMapper progressMapper;
    @Override
    public Application findApplication(Long studentId) {
        List<Application> applications = applicationMapper.findByStudentId(studentId);
        for(int i = 0;i<applications.size();i++){
            if(applications.get(i).getApplicationFinished().equals("未完成"))
                return applications.get(i);
        }
        return new Application();
    }

    @Override
    public List<Application> findAllApplication(Long studentId) {
        return applicationMapper.findByStudentId(studentId);
    }

    @Override
    public List<Application> findAllApplication() {
        return applicationMapper.findAllApplication();
    }

    @Override
    public Application addApplication(Application application, Long studentAccountId) {
//        String uniqueId = UUID.randomUUID().toString();
//        student.setStudentId(uniqueId);
        //新建学生获取学生id
        Student student = application.getStudent();
        if(application.getStudentId()==null) {
//            List<Student> students = studentMapper.selectList(null);
//            students.forEach(a -> {
//                a.setStudentId();
//            });
            studentMapper.insert(student);
            //学生与学员账号绑定
            studentAccountMapper.updateUserID(student.getStudentId(),studentAccountId);
        }
        //学生id存入申请表
        application.setStudentId(student.getStudentId());
        Application application1 = new Application();
        application1.setApplicationId(application.getApplicationId());
        application1.setStudentId(application.getStudentId());
        application1.setCoachId(application.getCoachId());
        application1.setLicenceId(application.getLicenceId());
        application1.setApplicationTime(application.getApplicationTime());
        application1.setApplicationPreference(application.getApplicationPreference());
        application1.setApplicationFinished(application.getApplicationFinished());
        int result = applicationMapper.insert(application1);
        if (result > 0) {
            //创建学习进度
            Progress p = new Progress();
            p.setApplicationId(application1.getApplicationId());

            p.setSubjectId(1L);
            p.setProgressState("无资格");
            progressMapper.insert(p);
            return application1;
        } else {
            throw new RuntimeException("Failed to add application");
        }
    }

    @Override
    public Long deleteApplication(Long applicationId) {
        int result = applicationMapper.deleteById(applicationId);
        if(result>0){
            return applicationId;
        }
        else{
            throw new RuntimeException("Failed to delete Application");
        }
    }

    @Override
    public Application updateApplication(Application application) {
        try {
            applicationMapper.updateApplication(application.getApplicationId(),application.getCoachId(),
                    application.getLicenceId(),application.getStudentId(),application.getApplicationTime(),
                    application.getApplicationPreference(),application.getApplicationFinished());
            System.out.println(application.toString());
            return application;
        }
        catch (Exception e){
            throw new RuntimeException("Failed to update Application");
        }
    }
}
